package com.stock.stockana.ui.activities;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.stock.publiclib.BaseActivity;
import com.stock.stockana.R;
import com.stock.stockana.StockCenter;
import com.stock.stockana.beans.ResponseBean;
import com.stock.stockana.beans.StockInfoBean;
import com.stock.stockana.constants.NetWorkConstants;
import com.stock.stockana.events.network.AddStockEvent;
import com.stock.stockana.events.network.DeleteStockEvent;
import com.stock.stockana.requests.AddStock;
import com.stock.stockana.requests.DeleteStock;
import com.stock.stockana.ui.fragments.StockDetailFragment;

import de.greenrobot.event.EventBus;
import de.greenrobot.event.Subscribe;
import uk.co.chrisjenx.calligraphy.CalligraphyContextWrapper;

/**
 * Created by ljgsonx on 2016/1/30.
 */
public class StockDetailActivity extends BaseActivity {

    private final static String TAG = "StockDetailActivity";

    private StockDetailFragment stockDetailFragment;

    private StockInfoBean clickedStock;

    public static StockDetailActivity instance;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        instance = this;
        EventBus.getDefault().register(this);
        clickedStock = (StockInfoBean) getIntent().getSerializableExtra("clickedStock");
        stockDetailFragment = new StockDetailFragment();
        Bundle bundle = new Bundle();
        bundle.putSerializable("clickedStock", clickedStock);
        stockDetailFragment.setArguments(bundle);
        addFragment(stockDetailFragment);
        setToolbarMenu();
        if(getSupportActionBar() != null){
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            onBackPressed();
        }
        return super.onOptionsItemSelected(item);
    }

    private void setToolbarMenu() {
        final TextView menuTv = getToolbarMenu();
        if (clickedStock.getNote() == StockInfoBean.STATUS_ADDED) {
            menuTv.setSelected(true);
            menuTv.setText(R.string.menu_hasCollected);
        }else{
            menuTv.setSelected(false);
            menuTv.setText(R.string.menu_notCollected);
        }
        getToolbarMenu().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (menuTv.isSelected()) {
                    DeleteStock deleteStock = DeleteStock.build().setStockNum(clickedStock.getStNum());
                    StockCenter.doReq(deleteStock);
                } else {
                    AddStock addStock = AddStock.build().setStockNum(clickedStock.getStNum());
                    StockCenter.doReq(addStock);
                }
            }
        });

    }

    /**
     * Description: 添加股票后,在这里接收网络返回的信息
     * Author: ljgsonx
     * Time: 2016/1/23 19:02
     */
    @Subscribe
    public void onEventMainThread(AddStockEvent event){
        ResponseBean rb = event.getmResponseBean();
        if (rb.getRespCode() == NetWorkConstants.RESPONSE_CODE_OK) {
            getToolbarMenu().setSelected(true);
            getToolbarMenu().setText(R.string.menu_hasCollected);
            stockDetailFragment.enableSureSettingBtn(true);
        }else {
            Log.d(TAG, rb.getErrorMsg());
            Toast.makeText(this, "添加失败!", Toast.LENGTH_SHORT).show();
        }
    }

    /**
     * Description: 删除股票后,在这里接收网络返回的信息
     * Author: ljgsonx
     * Time: 2016/1/23 19:02
     */
    @Subscribe
    public void onEventMainThread(DeleteStockEvent event){
        ResponseBean rb = event.getmResponseBean();
        if (rb.getRespCode() == NetWorkConstants.RESPONSE_CODE_OK) {
            getToolbarMenu().setSelected(false);
            getToolbarMenu().setText(R.string.menu_notCollected);
            stockDetailFragment.enableSureSettingBtn(false);
        }else {
            Log.d(TAG, rb.getErrorMsg());
            Toast.makeText(this, "取消失败!", Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onBackPressed() {
        if(getSupportFragmentManager().getBackStackEntryCount() > 1) {
            getSupportFragmentManager().popBackStackImmediate();
        }else{
            finish();
            instance = null;
            overridePendingTransition(R.anim.in_from_left, R.anim.out_from_right);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    /**
     * Description: 设置默认字体
     * Author: ljgsonx
     * Time: 2016/2/24 13:58
     */
    @Override
    protected void attachBaseContext(Context newBase) {
        super.attachBaseContext(CalligraphyContextWrapper.wrap(newBase));
    }
}
